package com.zbest.common.lang.enums;

import java.io.Serializable;

import com.zbest.common.lang.base.AbstractCodedEnum;
import com.zbest.common.lang.base.AbstractEnum;

/**
 * 业务编码，主要用于业务流程中提示操作码，例如验证异常、网络异常、数据库异常等.<br/>
 * <p/>
 * <pre>
 *     如果需要增加服务特有的业务码，则可以通过继承{@link com.zbest.common.lang.enums.BizCode}实现.
 * </pre>
 * <p/>
 * Created by wu.charles on 2014/12/10.
 */
public class BizCode extends AbstractCodedEnum implements Serializable {
    private static final long   serialVersionUID   = -6584286222918369706L;

    public static final BizCode SUCCESS            = new BizCode("SUCCESS", SystemCode.DEFAULT,
                                                       "000", "成功");

    public static final BizCode UNKNOWN            = new BizCode("UNKNOWN", SystemCode.DEFAULT,
                                                       "999", "未知错误");

    public static final BizCode SYSTEM_EXCEPTION   = new BizCode("SYSTEM_EXCEPTION",
                                                       SystemCode.DEFAULT, "900", "系统异常");

    public static final BizCode NO_PROCESS_MATCHED = new BizCode("NO_PROCESS_MATCHED",
                                                       SystemCode.DEFAULT, "001", "没有匹配的流程");

    public static final BizCode PARAM_IS_NULL      = new BizCode("PARAM_IS_NULL", SystemCode.DEFAULT,
                                                       "002", "参数必须为null值");

    public static final BizCode PARAM_TYPE_ERROR   = new BizCode("PARAM_Type_ERROR",
                                                       SystemCode.DEFAULT, "003", "参数类型不正确");

    public static final BizCode PARAM_ERROR        = new BizCode("PARAM_ERROR", SystemCode.DEFAULT,
                                                       "004", "参数不合法");

    public static final BizCode PARAM_NOT_EQUAL    = new BizCode("PARAM_NOT_EQUAL",
                                                       SystemCode.DEFAULT, "005", "参数不相等");

    public static final BizCode DATA_NOT_EXISTS    = new BizCode("DATA_NOT_EXISTS",
                                                       SystemCode.DEFAULT, "006", "数据不存在");

    public static final BizCode DUPLICATED_ERROR   = new BizCode("DUPLICATED_ERROR",
                                                       SystemCode.DEFAULT, "007", "数据重复");

    public static final BizCode DATA_ROW_ERROR     = new BizCode("DATA_ROW_ERROR",
                                                       SystemCode.DEFAULT, "008", "数据行数错误");

    public static final BizCode USER_NOT_LOGIN     = new BizCode("USER_NOT_LOGIN",
                                                       SystemCode.DEFAULT, "009", "用户未登录");

    public static final BizCode MONEY_NOT_EQUAL    = new BizCode("MONEY_NOT_EQUAL",
                                                       SystemCode.DEFAULT, "010", "金额不相等 ");

    // --------------------------- 属性
    private SystemCode          service            = null;                                   // 服务编码

    protected BizCode() {
        ; // 解决反序列化无法构造新实例的问题！！
    }

    public BizCode(String name, SystemCode service, String code, String desc) {
        super(service.code() + name, service.code() + code, desc);

        validateCode(code);

        this.service = service;
    }

    private void validateCode(String code) {
        if (code == null || code.isEmpty()) {
            throw new IllegalArgumentException("code不能为空");
        }

        if (code.length() != 3) {
            throw new IllegalArgumentException("code长度必须为3");
        }
    }

    @Override
    protected final Class<? extends AbstractEnum> getEnumType() {
        return BizCode.class;
    }

    public final SystemCode service() {
        return service;
    }
}
